package cn.xc.basic.controller;

import cn.xc.basic.dto.SmsCodeDto;
import cn.xc.basic.exception.BusinessException;
import cn.xc.basic.service.IVerifyCodeSerivce;
import cn.xc.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeSerivce verifyCodeSerivce;

    @GetMapping("/image/{key}")
    public AjaxResult imgKey(@PathVariable("key") String key){
        try {
            String base64Str = verifyCodeSerivce.imgKey(key);
            return AjaxResult.me().setResultObj(base64Str);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("网络异常，请稍后再试!");
        }
    }

    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody SmsCodeDto smsCodeDto){
        try {
            verifyCodeSerivce.smsCode(smsCodeDto);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络异常，请稍后再试！");
        }
    }

}
